
package com.nyist.myticket.web.common.controller;


import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSON;
import com.nyist.myticket.biz.entity.JobTask;
import com.nyist.myticket.biz.service.TbJobTaskService;
import com.nyist.myticket.core.enums.DataStatusEnum;
import com.nyist.myticket.core.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yangxiaogen
 * @since 2020-12-31
 */
@RestController
@RequestMapping("/quartz")
public class TbJobTaskController {

    private static Logger logger = LoggerFactory.getLogger(TbJobTaskController.class);

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private TbJobTaskService taskService;


    /**
     * 创建定时任务
     *
     * @param jobTask
     */
    @RequestMapping(value = "/createJob",method = RequestMethod.POST)
    public int createJob(@RequestBody JobTask jobTask) {
        logger.info("===============开始创建任务==============");
        QuartzUtils.createScheduleJob(scheduler, jobTask);
        taskService.saveJob(jobTask);
        logger.info("===============创建任务成功,信息:{}", JSON.toJSONString(jobTask));
        return HttpStatus.HTTP_OK;
    }


    /**
     * 删除定时任务
     *
     * @param jobTask
     */
    @RequestMapping(value = "/deleteJob",method = RequestMethod.POST)
    public int deleteJob(@RequestBody JobTask jobTask) {
        logger.info("=========删除定时任务，请求参数：{}", JSON.toJSONString(jobTask));
        boolean bool = taskService.deleteToChangeStatus(jobTask);
        if (bool) {
            QuartzUtils.deleteScheduleJob(scheduler, jobTask.getJobName());
        }
        logger.info("=========删除定时任务成功=========");
        return HttpStatus.HTTP_OK;
    }


    /**
     * 暂停任务
     *
     * @param jobTask
     */
    @RequestMapping("/pauseJob")
    public int pauseJob(@RequestBody JobTask jobTask) {
        logger.info("=========开始暂停任务，请求参数：{}=========", JSON.toJSONString(jobTask));
        QuartzUtils.pauseScheduleJob(scheduler, jobTask.getJobName());
        jobTask.setStatus(DataStatusEnum.DISABLE.getCode());
        taskService.updateJob(jobTask);
        logger.info("=========暂停任务成功=========");
        return HttpStatus.HTTP_OK;
    }

    /**
     * 立即运行一次定时任务
     *
     * @param jobTask
     */
    @RequestMapping("/runOnce")
    public int runOnce(@RequestBody JobTask jobTask) {
        logger.info("=========立即运行一次定时任务，请求参数：{}", JSON.toJSONString(jobTask));
        QuartzUtils.runOnce(scheduler, jobTask.getJobName());
        logger.info("=========立即运行一次定时任务成功=========");
        return HttpStatus.HTTP_OK;
    }

    /**
     * 恢复定时任务
     * @param jobTask <p>
     * 更新定时任务
     */
    @RequestMapping("/resume")
    public int resume(@RequestBody JobTask jobTask) {
        logger.info("=========恢复定时任务，请求参数：{}", JSON.toJSONString(jobTask));
        QuartzUtils.resumeScheduleJob(scheduler, jobTask.getJobName());
        jobTask.setStatus(DataStatusEnum.NORMAL.getCode());
        taskService.updateJob(jobTask);
        logger.info("=========恢复定时任务成功=========");
        return HttpStatus.HTTP_OK;
    }

    /**
     * 更新定时任务
     *
     * @param jobTask
     */
    @RequestMapping("/update")
    public int update(@RequestBody JobTask jobTask) {
        logger.info("=========更新定时任务，请求参数：{}", JSON.toJSONString(jobTask));
        QuartzUtils.updateScheduleJob(scheduler, jobTask);
        taskService.updateJob(jobTask);
        logger.info("=========更新定时任务成功=========");
        return HttpStatus.HTTP_OK;
    }

}
